package com.grt192.controller.hauntedhouse;

import com.grt192.actuator.GRTLED;
import com.grt192.core.EventController;
import com.grt192.event.component.SwitchListener;
import com.grt192.mechanism.hauntedhouse.HauntedHouseMechanism;
import com.grt192.mechanism.hauntedhouse.HHLEDMechanism;
import com.grt192.sensor.GRTSwitch;

/**
 * Master and Auto button state LED control
 * @author data, ajc, nik.lele
 */
public class HHLEDController extends EventController implements SwitchListener {

    public static final String MECHANISM_ID = "HHLED";

    public static final boolean MASTER_DEFAULT_STATE = true;
    public static final boolean AUTO_DEFAULT_STATE = true;
    //private HauntedHouseController[] hhcs;
    private HHLEDMechanism om;
    private GRTSwitch masterSwitch;
    private GRTSwitch autoSwitch;
    private boolean master;
    private boolean auto;

    public HHLEDController() {
        initMechanism();

        //set default LED states
        master = MASTER_DEFAULT_STATE;
        auto = AUTO_DEFAULT_STATE;
    }

    public void initMechanism(){
        om = new HHLEDMechanism();
        addMechanism(MECHANISM_ID, om);
    }

    public void addListeners() {
        masterSwitch = (GRTSwitch) om.getSensor(HHLEDMechanism.MASTER_BUTTON_ID);
        autoSwitch = (GRTSwitch) om.getSensor(HHLEDMechanism.AUTO_BUTTON_ID);

        masterSwitch.addSwitchListener(this);
        autoSwitch.addSwitchListener(this);

        //TODO check if this was what worked-- both this and the
        //block above were implemented simultaneously
//        masterSwitch = (GRTSwitch) om.getSensor("MasterButton");
//        autoSwitch = (GRTSwitch) om.getSensor("AutoButton");
//        masterSwitch.addSwitchListener(this);
//        autoSwitch.addSwitchListener(this);
    }

    public void switchPressed(GRTSwitch source) {
        System.out.println("OperatorControl switch press");
    }

    public void switchReleased(GRTSwitch source) {
        System.out.println("OperatorControl switch release");
        if (source == autoSwitch) {
            auto = !auto;
            om.setAutoLED(auto);
            System.out.println("Auto switch to " + auto);
        }
        if (source == masterSwitch) {
            master = !master;
            om.setMasterLED(master);
            System.out.println("Master switch to " + master);
        }
    }
}
